package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleFreeMarkerService;
import com.heima.common.constants.ArticleConstants;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.search.vos.SearchArticleVo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class ApArticleFreeMarkerServiceImpl implements ApArticleFreeMarkerService {
    @Autowired
    private Configuration configuration;

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    /**
     * 生成文章静态页面并保存到MinIO中
     *
     * @param content 文章内容
     */
    @Override
    @Async // 该方法是一个异步调用方法
    public void buildArticleStaticPageToMinIO(String content, ApArticle apArticle) {

        // 判断ApArticleContent是否为空
        if (StringUtils.isNotEmpty(content)) {
            // 文章内容通过freemarker生成HTML文件
            StringWriter out = new StringWriter();
            try {
                Template template = configuration.getTemplate("article.ftl");
                // 创建Map集合
                Map<String, Object> map = new HashMap<>();
                map.put("content", JSONArray.parseArray(content));

                template.process(map, out);
            } catch (Exception e) {
                e.printStackTrace();
            }

            InputStream is = new ByteArrayInputStream(out.toString().getBytes());

            // 将html文件上传到minio中
            String path = fileStorageService.uploadHtmlFile("", apArticle.getId() + ".html", is);

            // 将文件路径更新到ap_article表中
            apArticle.setStaticUrl(path);
            apArticleMapper.updateById(apArticle);

            // 发送消息,同步es数据
            syncArticleToEs(apArticle, content);
        }
    }

    /**
     * 发送消息同步数据到es
     *
     * @param apArticle
     * @param content
     */
    public void syncArticleToEs(ApArticle apArticle, String content) {
        SearchArticleVo articleVo = new SearchArticleVo();
        // 属性拷贝
        BeanUtils.copyProperties(apArticle, articleVo);
        articleVo.setContent(content);

        // 发送消息
        kafkaTemplate.send(ArticleConstants.ARTICLE_ES_SYNC_TOPIC, JSON.toJSONString(articleVo));
    }
}
